﻿using SHDM.Web.DataAccess;
using SHDM.Web.Infrastructure;
using SHDM.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace SHDM.Web.Controllers
{
    [Authenticator]
    [RoutePrefix("api/dept")]
    public class DeptController : ApiBaseController
    {
        [HttpGet]
        [Route("GetDepts")]
        public Result GetDepts()
        {
            var lst = new SHDMEntities().sh_dept.ToList();
            return new Result() { Data=lst };
        }

        [HttpPost]
        [Route("Edit")]
        public Result Edit(sh_dept model)
        {
            if (this.AccountInfo.Role != sh_users.Roles.Admin)
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "没有权限" };
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "部门名称不能为空" };
            }
            var db = new SHDMEntities();
            if(model.ParentID != 0 && !db.sh_dept.Any(e=>e.ID == model.ParentID))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "父部门不存在" };
            }
            
            if(model.ID > 0)
            {
                if (model.ParentID == model.ID)
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "父部门不能为自身" };
                }
                var dept = db.sh_dept.FirstOrDefault(e => e.ID == model.ID);
                if (dept == null)
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "部门不存在" };
                }
                var lstChildren = GetChildren(model.ID);
                if (lstChildren != null && lstChildren.Any(e => e.ID == model.ParentID && e.ID != model.ID))
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "父部门不能为本部门的子部门" };
                }
                
                dept.Name = model.Name;
                dept.ParentID = model.ParentID;
                dept.State = model.State;
                db.SaveChanges();
            }
            else
            {
                db.sh_dept.Add(model);
                db.SaveChanges();
            }
            return new Result() { ErrorCode = 0, ErrorMessage = "操作成功" };
        }

        [HttpGet]
        [Route("Delete")]
        public Result Delete(int id)
        {
            if (this.AccountInfo.Role != sh_users.Roles.Admin)
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "没有权限" };
            }
            var db = new SHDMEntities();
            if(db.sh_dept.Any(e=>e.ParentID == id))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "部门存在子部门,不能删除" };
            }

            if (db.sh_users.Any(e => e.DeptID == id))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "部门存在人员,不能删除" };
            }

            if (db.sh_folders.Any(e => e.DeptID == id))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "部门存在文件夹,不能删除" };
            }

            var dept = db.sh_dept.FirstOrDefault(e => e.ID == id);
            if(dept == null)
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "部门不存在" };
            }
            db.sh_dept.Remove(dept);
            db.SaveChanges();
            return new Result() { ErrorMessage = "操作成功" };
        }

        List<sh_dept> GetParent(int id)
        {
            return new SHDMEntities().Database.SqlQuery<sh_dept>($"call getDeptParent({id})").ToList();
        }

        List<sh_dept> GetChildren(int id)
        {
            return new SHDMEntities().Database.SqlQuery<sh_dept>($"select * from sh_dept t where FIND_IN_SET(id,queryChildrenDeptInfo({id}))").ToList();
        }
    }
}